import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: UserList(),
      appBar: AppBar(
        title: Text("StatefulWidget"),
      ),
    );
  }
}

//DataTable 表格
class UserList extends StatefulWidget {
  UserList({Key? key}) : super(key: key);

  @override
  State<UserList> createState() => UserListState();
}

class UserListState extends State<UserList> {
  List<User> users = [
    User('张三', 18),
    User('张三封', 180, selected: true),
    User('张翠山', 28),
    User('张五级', 68),
  ];

  List<DataRow> _getDataColumn() {
    List<DataRow> list = [];
    for (var i = 0; i < users.length; i++) {
      list.add(DataRow(
          selected: users[i].selected,
          onSelectChanged: (value) {
            setState(() {
              users[i].selected = value!;
            });
          },
          cells: [
            DataCell(Text(users[i].name)),
            DataCell(Text('${users[i].age}')),
            DataCell(Text('男')),
            DataCell(Text('武当山江湖人士',))
          ]));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: DataTable(
          headingRowHeight: 30,
          
          horizontalMargin: 40,
          dataTextStyle:TextStyle(fontSize: 20, color: Colors.blueAccent),

          columns: [
            DataColumn(label: Text('姓名', textAlign:TextAlign.center)),
            DataColumn(label: Text('年龄')),
            DataColumn(label: Text('性别')),
            DataColumn(label: Text('简介',textAlign:TextAlign.center)),
          ],
          rows: _getDataColumn(),
        ),
      ),
    );
  }
}

class User {
  String name;
  int age;
  bool selected;
  User(this.name, this.age, {this.selected = false});
}

/*
//简单的 表格组件使用
class DataTableDemo extends StatefulWidget {
  const DataTableDemo({ Key? key }) : super(key: key);

  @override
  State<DataTableDemo> createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: DataTable(
        columns: [
          DataColumn(label: Text('姓名')),
          DataColumn(label: Text('年龄')),
          DataColumn(label: Text('简介')),
        ],
        rows: [
          DataRow(
            cells: [
              DataCell(Text("张三")),
              DataCell(Text("18")),
              DataCell(Text('一个有故事的人, 运动达人,老家在快乐星球,真棒'))
            ],
          ),
           DataRow(
            cells: [
              DataCell(Text("李四")),
              DataCell(Text("20")),
              DataCell(Text('吃货, 宅男, LOL爱好者'))
            ],
          ),
        ],
      ),
    );
  }
}
*/
